/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.profile;

import org.openaecfm.model.profile.ReinforcementBarProperties;
import org.openaecfm.model.profile.ReinforcingBarSurfaceEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ReinforcementBarPropertiesIO extends
		AbstractEntityInstanceInputImpl<ReinforcementBarProperties> implements
		IEntityInstanceOutput<ReinforcementBarProperties> {

	@Override
	public String exportSPF(ReinforcementBarProperties pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCREINFORCEMENTBARPROPERTIES", appDataManager.getName(pojo));

		// Set TotalCrossSectionArea
		ei.addParameter(new RealParameter(pojo.getTotalCrossSectionArea()));

		// Set SteelGrade
		ei.addParameter(new StringParameter(pojo.getSteelGrade(),
				StringParameter.UTF_8));

		// Set BarSurface
		if (pojo.getBarSurface() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getBarSurface()));
		}

		// Set EffectiveDepth
		if (pojo.getEffectiveDepth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getEffectiveDepth()));
		}

		// Set NominalBarDiameter
		if (pojo.getNominalBarDiameter() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getNominalBarDiameter()));
		}

		// Set BarCount
		if (pojo.getBarCount() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getBarCount()));
		}

		return ei.toString();
	}

	@Override
	protected ReinforcementBarProperties doCreateObject() {
		return new ReinforcementBarProperties();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ReinforcementBarProperties pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TotalCrossSectionArea
		RealParameter totalCrossSectionArea = (RealParameter) sei
				.getParameter(0);
		pojo.setTotalCrossSectionArea(totalCrossSectionArea.toDouble());

		// Set SteelGrade
		StringParameter steelGrade = (StringParameter) sei.getParameter(1);
		pojo.setSteelGrade(steelGrade.toUTF8());

		// Set BarSurface
		Parameter barSurface = sei.getParameter(2);
		if (barSurface.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) barSurface;
			pojo.setBarSurface(ReinforcingBarSurfaceEnum.valueOf(e
					.toEnumString()));
		}

		// Set EffectiveDepth
		Parameter effectiveDepth = sei.getParameter(3);
		if (effectiveDepth.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) effectiveDepth;
			pojo.setEffectiveDepth(r.toDouble());
		}

		// Set NominalBarDiameter
		Parameter nominalBarDiameter = sei.getParameter(4);
		if (nominalBarDiameter.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) nominalBarDiameter;
			pojo.setNominalBarDiameter(r.toDouble());
		}

		// Set BarCount
		Parameter barCount = sei.getParameter(5);
		if (barCount.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) barCount;
			pojo.setBarCount(r.toDouble());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			ReinforcementBarProperties pojo, IAppDataManager appDataManager) {
		return;
	}
}
